package labuladong.leetcode.editor.cn._03binarytree.ch00;

public class _543_DiameterOfBinaryTree {

    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        int maxDiameter = 0;

        public int diameterOfBinaryTree(TreeNode root) {
            maxDepth(root);
            return maxDiameter;
        }

        int maxDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int leftMax = maxDepth(root.left);
            int rightMax = maxDepth(root.right);
            // 后序遍历位置顺便计算最大直径
            maxDiameter = Math.max(maxDiameter, leftMax + rightMax);
            return 1 + Math.max(leftMax, rightMax);
        }
    }

    // 这是一种简单粗暴，但是效率不高的解法
    class BadSolution {
        public int diameterOfBinaryTree(TreeNode root) {
            if (root == null) {
                return 0;
            }
            // 计算出左右子树的最大高度
            int leftMax = maxDepth(root.left);
            int rightMax = maxDepth(root.right);
            // root 这个节点的直径
            int res = leftMax + rightMax;
            // 递归遍历 root.left 和 root.right 两个子树
            return Math.max(res,
                    Math.max(diameterOfBinaryTree(root.left),
                            diameterOfBinaryTree(root.right)));
        }

        int maxDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int leftMax = maxDepth(root.left);
            int rightMax = maxDepth(root.right);
            return 1 + Math.max(leftMax, rightMax);
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static void main(String[] args) {
        Solution solution = new _543_DiameterOfBinaryTree().new Solution();

        TreeNode n4 = new TreeNode(4, null, null);
        TreeNode n5 = new TreeNode(5);
        TreeNode n3 = new TreeNode(3);
        TreeNode n2 = new TreeNode(2, n4, n5);
        TreeNode n1 = new TreeNode(1, n2, n3);

        System.out.println(solution.diameterOfBinaryTree(n1));
    }
}
